package com.itcast.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.dao.RoleMapper;
import com.itcast.exception.RoleException;
import com.itcast.health.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 添加角色
     *
     * @param role
     */
    @Override
    public void add(Role role) {
        /*添加角色*/
        roleMapper.add(role);
        Integer id = role.getId();
        /*获取权限id集合*/
        List<Integer> permissionIds = role.getPermissionIds();
        /*获取菜单id集合*/
        List<Integer> menuIds = role.getMenuIds();
        /*循环插入角色_权限表*/
        for (Integer permissionId : permissionIds) {
            roleMapper.addRoleWithPermission(id, permissionId);
        }
        /*循环插入角色_菜单表*/
        for (Integer menuId : menuIds) {
            roleMapper.addRoleWithMenu(id, menuId);
        }
    }

    /**
     * 查询所有权限
     *
     * @return
     */
    @Override
    public Result findAllPermission() {
        List<Permission> permissions = roleMapper.findAllPermission();
        if (CollectionUtil.isNotEmpty(permissions)) {
            return new Result(true, "", permissions);
        }
        return new Result(false, "查询角色权限异常,请联系管理员或刷新后重试");
    }

    /**
     * 查询所有菜单
     *
     * @return
     */
    @Override
    public Result findAllMenu() {
        List<Menu> menus = roleMapper.findAllMenu();
        if (CollectionUtil.isNotEmpty(menus)) {
            return new Result(true, "", menus);
        }
        return new Result(false, "查询菜单异常,请联系管理员或刷新后重试");
    }

    /**
     * 数据分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        System.out.println(queryPageBean.getCurrentPage());
        System.out.println(queryPageBean.getPageSize());
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Role> roleList = roleMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), roleList);
    }

    /**
     * 编辑窗口,数据回显
     *
     * @param id
     * @return
     */
    @Override
    public Role findById(Integer id) {
        /*角色信息回显*/
        Role role = roleMapper.findById(id);
        /*权限信息回显*/
        List<Permission> allPermission = roleMapper.findAllPermission();
        /*菜单信息回显*/
        List<Menu> allMenu = roleMapper.findAllMenu();
        /*权限信息选中状态回显*/
        List<Integer> permissionIdList = roleMapper.findPermissionHasChosed(id);
        /*菜单信息选中状态回显*/
        List<Integer> menuIdList = roleMapper.findMenuHasChosed(id);
        role.setPermissionsList(allPermission);
        role.setMenusList(allMenu);
        role.setPermissionIds(permissionIdList);
        role.setMenuIds(menuIdList);
        return role;
    }

    /**
     * 编辑角色
     *
     * @param role
     */
    @Override
    public void edit(Role role) {
        roleMapper.deleteRPById(role.getId());
        roleMapper.deleteRMById(role.getId());
        roleMapper.edit(role);
        List<Integer> permissionIds = role.getPermissionIds();
        List<Integer> menuIds = role.getMenuIds();
        for (Integer permissionId : permissionIds) {
            roleMapper.addRoleWithPermission(role.getId(), permissionId);
        }
        for (Integer menuId : menuIds) {
            roleMapper.addRoleWithMenu(role.getId(), menuId);
        }
    }

    /**
     * 删除角色
     * @param id
     */
    @Override
    public void delete(Integer id) {
        int i = roleMapper.findUserWithRoleId(id);
        if (i >= 1) {
            throw new RoleException("存在引用,无法删除");
        } else {
            roleMapper.deleteRPById(id);
            roleMapper.deleteRMById(id);
            roleMapper.deleteById(id);
        }
    }
}
